/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.annotated;

import junit.framework.AssertionFailedError;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.sourceprojects.lycia.test.TestCaseUtilities;

import static org.sourceprojects.lycia.fluent.FluentBuilder.*;

public class BooleanWrapperConvertTestCase {

	private static final String XML_SIMPLE = TestCaseUtilities.XML_HEADER
			+ "<test><testcase-simple value=\"true\" /></test>";
	private static final String XML_DEFAULT = TestCaseUtilities.XML_HEADER
			+ "<test><testcase-default /></test>";

	@Test
	public void testSimpleBooleanConvert() throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.parser(
				tag("testcase-simple", method("simpleBooleanConvert", this,
						Boolean.class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test(expected = LyciaParsingException.class)
	public void testDefaultBooleanConvertNullAware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(true)).parser(
				tag("testcase-default", method("defaultBooleanConvert", this,
						Boolean.class))).build();

		parser.parse(XML_DEFAULT);
	}

	@Test
	public void testDefaultBooleanConvertNullUnaware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false))
				.parser(
						tag("testcase-default", method("defaultBooleanConvert",
								this, Boolean.class))).build();

		parser.parse(XML_DEFAULT);
	}

	@Test
	public void testGivenDefaultBooleanConvertNullAware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(true)).parser(
				tag("testcase-default", method("givenDefaultBooleanConvert",
						this, Boolean.class))).build();

		parser.parse(XML_DEFAULT);
	}

	@Test
	public void testGivenDefaultBooleanConvertNullUnaware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.parser(
				tag("testcase-default", method("givenDefaultBooleanConvert",
						this, Boolean.class))).build();

		parser.parse(XML_DEFAULT);
	}

	public void simpleBooleanConvert(@Attribute("value") Boolean value) {
		if (!value)
			throw new AssertionFailedError("Expected true but was false");
	}

	public void defaultBooleanConvert(@Attribute("value") Boolean value) {
		if (value != null)
			throw new AssertionFailedError("Expected null but was true");
	}

	public void givenDefaultBooleanConvert(
			@Attribute(value = "value", defaultValue = "true") Boolean value) {

		if (!value)
			throw new AssertionFailedError("Expected true but was false");
	}

}
